/*
 * DrieBuffer.cpp
 *
 *  Created on: Mar 29, 2012
 *      Author: s146913
 */

#include "DrieBuffer.h"

DrieBuffer::DrieBuffer(Buffer* ap, Buffer* bp, Buffer* cp) :
	Monitor("DrieBuffer"),
	readp(ap), prevp(bp), fillp(cp), readPossible(false), isFirstFill(true) { }

DrieBuffer::~DrieBuffer() {
	delete readp;
	delete prevp;
	delete fillp;
}

Buffer* DrieBuffer::getReadBuffer() {
	ENTER();
	if(!readPossible) {
		WAIT();
	}

	switchBuffer(readp, prevp);
	readPossible = false;

	LEAVE();
	return readp;
}

Buffer* DrieBuffer::getFillBuffer(){
	ENTER();

	if(!isFirstFill) {
	switchBuffer(fillp, prevp);
	readPossible = true;
	NOTIFY();
	}
	isFirstFill = false;
	LEAVE();
	return fillp;
}

void DrieBuffer::switchBuffer(Buffer* & a, Buffer* & b) {
	Buffer* tempBuff = a;
	a = b;
	b = tempBuff;
}
